package ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;

import ui.handlers.PayHandler;


import java.rmi.RemoteException;
import java.util.ArrayList;

public class PayTransactionScreen extends BaseScreen{

	private JButton pay_btn;
	private JButton cancel_btn;
	private JFrame mainFrame;
	private JTextField cash_tf, credit_tf, expDate_tf;
	private JRadioButton cash_rb, credit_rb;
	
	int transactionID;
	
	/* */
	
	public JButton getPayButton()
	{ return pay_btn; }
	
	public JButton getCancelButton()
	{ return cancel_btn; }
	
	public JRadioButton getCashRButton()
	{ return cash_rb; }
	
	public JRadioButton getCreditRButton()
	{ return credit_rb; }
	
	public String getCashText()
	{ return cash_tf.getText(); }
	
	public String getCreditText()
	{ return credit_tf.getText(); }
	
	public String getExpDateText()
	{ return expDate_tf.getText(); }
	
	public JFrame getMainFrame()
	{ return mainFrame; }
	
	/* */
	
	public PayTransactionScreen(int transactionID)
	{
		this.transactionID = transactionID;
		
		mainFrame = new JFrame();
		mainFrame.setTitle("Payment");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		doTitle();
		doName();
		try {
			doMainContent();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		doBottom();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String title = "<HTML><FONT SIZE = \"6\">" +
			"Order</FONT></HTML>";
		JLabel title_lbl = new JLabel(title);
		title_lbl.setBorder(new EmptyBorder(10, 10, 0, 0));
		mainFrame.add(title_lbl, BorderLayout.NORTH);
	}
	
	private void doMainContent() throws RemoteException
	{
		ArrayList<Integer> orderList = store.getPendingTransactionItems(transactionID);
		
		JPanel order_panel = new JPanel();
		JPanel list_panel = new JPanel();
		list_panel.setLayout(new GridLayout(orderList.size()+2, 1));
		
		for (int orderItemID : orderList)
		{
			JPanel temp_panel = new JPanel();
			temp_panel.setLayout(new GridLayout(1, 3));
			
			
			JLabel name_lbl = new JLabel(store.getMenuItemName(orderItemID));
			JLabel quantity_lbl = new JLabel(""+store.getTransactionItemQuantity(transactionID, orderItemID));
			JLabel price_lbl = new JLabel(""+store.getMenuItemPrice(orderItemID));
			
			temp_panel.add(name_lbl);
			temp_panel.add(quantity_lbl);
			temp_panel.add(price_lbl);
			
			list_panel.add(temp_panel);
		}
		
		JPanel total_panel = new JPanel();
		total_panel.setLayout(new GridLayout(1, 3));
		total_panel.add(new JPanel());
		total_panel.add(new JLabel("Total:"));
		total_panel.add(new JLabel(""+store.getTransacationTotal(transactionID)));;
		
		list_panel.add(new JLabel("_____________________________________________"));
		list_panel.add(total_panel);		
		order_panel.add(list_panel);
		mainFrame.add(order_panel, BorderLayout.WEST);
	}
	
	private void doName()
	{
		String text = "";
		try {
			text = "<HTML><FONT SIZE = \"4\">"+
					"Customer: "+ store.getCustomerNameByTransaction(transactionID) +"</FONT></HTML>";
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JLabel name_lbl = new JLabel(text);
		name_lbl.setBorder(new EmptyBorder(0, 10, 0, 30));
		mainFrame.add(name_lbl, BorderLayout.EAST);
	}
	
	private void doBottom()
	{
		pay_btn = new JButton("Pay");
		Font btnFont = new Font(pay_btn.getName(), pay_btn.getFont().getStyle(), 18);
		pay_btn.setFont(btnFont);
		pay_btn.addActionListener(new PayHandler(transactionID, this));
		
		//edit_btn = new JButton("Edit");
		//edit_btn.setFont(btnFont);
		
		cancel_btn = new JButton("Cancel");
		cancel_btn.setFont(btnFont);
		cancel_btn.addActionListener(new PayHandler(transactionID, this));
		
		cash_tf = new JTextField(10);
		credit_tf = new JTextField(15);
		expDate_tf = new JTextField(5);
		
		cash_rb = new JRadioButton();
		cash_rb.setSelected(true);
		credit_rb = new JRadioButton();
		
		ButtonGroup paymentType = new ButtonGroup();
		paymentType.add(cash_rb);
		paymentType.add(credit_rb);
		
		JPanel cash_panel = new JPanel();
		cash_panel.setLayout(new FlowLayout());
		cash_panel.setBorder(new EmptyBorder(0, 0, 0, 190));
		cash_panel.add(cash_rb);
		cash_panel.add(new JLabel("Cash Amount:"));
		cash_panel.add(cash_tf);
		
		JPanel credit_panel = new JPanel();
		credit_panel.setLayout(new FlowLayout());
		credit_panel.add(credit_rb);
		credit_panel.add(new JLabel("Credit card #:"));
		credit_panel.add(credit_tf);
		credit_panel.add(new JLabel("Exp-Date:"));
		credit_panel.add(expDate_tf);
		
		JPanel pay_panel = new JPanel();
		pay_panel.setLayout(new GridLayout(2, 1));
		pay_panel.setBorder(new EmptyBorder(0, 0, 30, 0));
		pay_panel.add(cash_panel);
		pay_panel.add(credit_panel);
		
		JPanel confirm_panel = new JPanel();
		confirm_panel.add(pay_btn);
		confirm_panel.setBorder(new EmptyBorder(0, 0, 0, 15));
		
		JPanel cancel_panel = new JPanel();
		cancel_panel.add(cancel_btn);
		cancel_panel.setBorder(new EmptyBorder(0, 15, 0, 0));
		
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		
		button_panel.add(confirm_panel);
		//button_panel.add(edit_panel);
		button_panel.add(cancel_panel);
		
		JPanel bottom_panel = new JPanel();
		bottom_panel.setLayout(new GridLayout(2, 1));
		bottom_panel.setBorder(new EmptyBorder(0, 0, 150, 0));
		bottom_panel.add(pay_panel);
		bottom_panel.add(button_panel);
		
		mainFrame.add(bottom_panel, BorderLayout.SOUTH);
	}
	
}